/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.ArrayList;
import java.util.regex.Pattern;

/**
 *
 * @author hxg
 */
public class StringUtils {

    static Pattern bitStrPattern = Pattern.compile("(\\d\\d[:])*\\d\\d");

    /**
     * 移除bs及前面一个字符,实现bs功能
     *
     * @param srcbytes
     * @return
     */
    public static byte[] removeBS(byte[] srcbytes) {
        byte bsbyte = 8;
        //ArrayList<Byte> bytelist=new ArrayList(str.length());
        //byte[] srcbytes = str.getBytes();
        byte[] bytes = new byte[srcbytes.length];
        int i = 0;
        for (byte b : srcbytes) {
            if (b == bsbyte && i > 0) {
                bytes[i - 1] = -1;
                i--;
            } else {
                bytes[i] = b;
                i++;
            }
        }
        byte[] resbyte = new byte[i + 1];
        System.arraycopy(bytes, 0, resbyte, 0, i);
        return resbyte;
    }

    public static String removeBS(String str) {
        byte bsbyte = 8;
        //ArrayList<Byte> bytelist=new ArrayList(str.length());
        byte[] srcbytes = str.getBytes();
        byte[] bytes = new byte[str.length()];
        int i = 0;
        for (byte b : srcbytes) {
            if (b == bsbyte && i > 0) {
                bytes[i - 1] = -1;
                i--;
            } else {
                bytes[i] = b;
                i++;
            }
        }
        byte[] resbyte = new byte[i + 1];
        System.arraycopy(bytes, 0, resbyte, 0, i);
        return new String(resbyte).trim();
    }

    public static String VT100removeChar2(String dealstr) {
        byte[] bytes = dealstr.getBytes();
        byte[] newbytes = new byte[bytes.length];
        for (int i = 0; i < newbytes.length; i++) {
            newbytes[i] = -1;
        }
        boolean scan = false;
        int point = 0, number = 0;
        char action;
        ArrayList<Byte> tailbuf = new ArrayList();
        for (byte b : bytes) {
            if (b == 27) {
                scan = true;
                number = 0;
                tailbuf.add(b);
            } else if (scan) {
                if (b >= 48 && b <= 57) {
                    number = number * 10 + (b - 48);
                }
                if ((b >= 65 && b <= 90) || (b >= 97 && b <= 122)) {
                    action = (char) b;
                    if (action == 'D') {
                        for (int i = 0; i < number; i++) {
                            newbytes[point - i] = -1;
                        }
                        point = point - number;
                    }
                    scan = false;
                    tailbuf.clear();
                }
                if (scan) {
                    tailbuf.add(b);
                }
            } else {
                newbytes[point] = b;
                point++;
            }
        }
        byte[] resbyte = new byte[point + tailbuf.size()];
        System.arraycopy(newbytes, 0, resbyte, 0, point);
        for (int i = 0; i < tailbuf.size(); i++) {
            resbyte[point + i] = tailbuf.get(i);
        }
        return new String(resbyte);
    }

    /**
     * 将格式为00：00：00格式的String转成普通String
     *
     * @return
     */
    public static String bitStringTrans(String input) {
        if (bitStrPattern.matcher(input).matches()) {
            try {
                String[] bstrs = input.split(":");
                byte[] bytes=new byte[bstrs.length];
                for(int i=0;i<bytes.length;i++){
                    bytes[i]=Byte.parseByte(bstrs[i]);
                }
                return new String(bytes);
            } catch (Exception e) {
                return input;
            }
        } else {
            return input;
        }
    }
}
